package ui {

	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import globals.Global;
	import globals.Setting;
	import globals.State;
	
	import ui.component.Tip;
	import ui.face.BottomFace;
	import ui.face.QuickBarFace;
	import ui.face.ScoreFace;
	import ui.face.UserFace;
	import ui.panel.BasePanel;
	import ui.panel.EquipPanel;
	import ui.panel.OptionPanel;
	import ui.panel.PresencePanel;
	import ui.panel.TowerPanel;
	import ui.panel.TransferPanel;
	import ui.panel.WarriorLevelUpPanel;
	import ui.panel.WarriorPanel;
	import ui.panel.WeaponPanel;
	
	import util.DisplayUtil;

	public class UIManager {
		
		public var userFace:UserFace;
		public var scoreFace:ScoreFace;
		public var bottomFace:BottomFace;
		public var quickBarFace:QuickBarFace;
		
		public var warriorPanel:WarriorPanel;
		public var weaponPanel:WeaponPanel;
		public var optionPanel:OptionPanel;
		public var towerPanel:TowerPanel;
		public var presencePanel:PresencePanel;
		public var equipPanel:EquipPanel;
		public var transferPanel:TransferPanel;
		public var warriorLevelupPanel:WarriorLevelUpPanel;
		
		private var maskLayer:Sprite;
		
		private var faces:Array;
		private var panels:Dictionary;
		
		private var tip:Tip;
		private var tipMap:Dictionary = new Dictionary();

		public function UIManager() {
			faces = new Array();
			panels = new Dictionary();
		}
		
		public function init():void {
			userFace = new UserFace();
			scoreFace = new ScoreFace();
			bottomFace = new BottomFace();
			quickBarFace = new QuickBarFace();
			Global.game.faceLayer.addChild(userFace);
			Global.game.faceLayer.addChild(scoreFace);
			Global.game.faceLayer.addChild(bottomFace);
			Global.game.faceLayer.addChild(quickBarFace);
			
			scoreFace.x = 260;
			bottomFace.y = 486;
			quickBarFace.x = 10;
			quickBarFace.y = 220;
			
			tip = new Tip();
			
			optionPanel = new OptionPanel();
			panels[optionPanel.pid] = optionPanel;
			
			towerPanel = new TowerPanel();
			panels[towerPanel.pid] = towerPanel;
			warriorPanel = new WarriorPanel();
			panels[warriorPanel.pid] = warriorPanel;
			weaponPanel = new WeaponPanel();
			panels[weaponPanel.pid] = weaponPanel;
			presencePanel = new PresencePanel();
			panels[presencePanel.pid] = presencePanel;
			equipPanel = new EquipPanel();
			panels[equipPanel.pid] = equipPanel;
			transferPanel = new TransferPanel();
			panels[transferPanel.pid] = transferPanel;
			warriorLevelupPanel = new WarriorLevelUpPanel();
			panels[warriorLevelupPanel.pid] = warriorLevelupPanel;
		}
		
		public function openPanel(id:uint):void {
			var panel:BasePanel = panels[id] as BasePanel;
			if (panel != null && !panel.opened) {
				panel.open();
			}
		}
		
		public function closePanel(id:uint):void {
			var panel:BasePanel = panels[id] as BasePanel;
			if (panel != null && panel.opened) {
				panel.close();
			}
		}
		
		public function addTip(target:DisplayObject, tip:String):void {
			target.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			target.addEventListener(MouseEvent.ROLL_OUT, outHandler);
			tipMap[target] = tip;
		}
		
		private function overHandler(event:MouseEvent):void {
			var target:DisplayObject = event.target as DisplayObject;
			if (tipMap[target] != null) {
				tip.update(tipMap[target]);
				tip.x = target.x + 9;
				tip.y = target.y + 4;
				Global.stage.addChild(tip);
			}
		}
		
		private function outHandler(event:MouseEvent):void {
			tip.update("");
			DisplayUtil.removeChild(Global.stage, tip);
		}
		
		public function clear():void {
			for each (var panel:BasePanel in panels) {
				panel.close();
			}
		}
		
		public function showMaskLayer():void {
			if (maskLayer == null) {
				maskLayer = new Sprite();
				maskLayer.graphics.beginFill(0x666666, 0.5);
				maskLayer.graphics.drawRect(0, 0, Setting.STAGE_WIDTH, Setting.STAGE_HEIGHT);
				maskLayer.graphics.endFill();
			}
			Global.game.panelLayer.addChild(maskLayer);
		}
		
		public function hideMaskLayer():void {
			DisplayUtil.removeChild(Global.game.panelLayer, maskLayer);
		}
		
		public function enterBuildState():void {
			var state:uint = Global.stateManager.getState();
			if (state == State.STATE_NORMAL) {
				Global.mapManager.arena.showBuildGrid();
				Global.stateManager.enterState(State.STATE_BUILD);
			}
		}
		
		public function exitBuildState():void {
			var state:uint = Global.stateManager.getState();
			if (state == State.STATE_BUILD) {
				Global.mapManager.arena.hideBuildGrid();
				Global.stateManager.enterState(State.STATE_NORMAL);
			}
		}
		
		public function showRange(x:Number, y:Number, range:Number):void {
			var g:Graphics = Global.game.topLayer.graphics;
			g.clear();
			g.beginFill(0x000000, 0.5);
			g.drawCircle(x, y, range);
			g.endFill();
		}
		
		public function hideRange():void {
			Global.game.topLayer.graphics.clear();
		}
	}
}